<%@ page contentType="image/jpeg" import="com.zx.platform.verifycode.IVerifyCode,com.zx.platform.verifycode.VerifyCodeHelper,javax.imageio.ImageIO,java.awt.*" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="java.util.Random" %>
<%!
    Color getRandColor(int fc, int bc) {//得到随机颜色
        Random random = new Random();
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }
%><%
    //设置返回头
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);

    IVerifyCode code = (IVerifyCode) session.getAttribute(VerifyCodeHelper.VERIFYCODE_BINDKEY);

    //设置图片大小
    int width = 20 * code.getCode().length(), height = 36;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    //新建对象
    Graphics g = image.getGraphics();

    //随机
    Random random = new Random();

    //设置颜色
    g.setColor(getRandColor(200, 250));
    g.fillRect(0, 0, width, height);

    //设置字体
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

    //设置颜色
    g.setColor(Color.white);
    g.drawRect(0, 0, width - 1, height - 1);

    //设置颜色
    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 155; i++) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
        g.drawLine(x, y, x + xl, y + yl);
    }

    //随机
    String sRand = code.getCode();
    for (int i = 0; i < sRand.length(); i++) {
        String rand = sRand.substring(i, i + 1);
//        sRand += rand;

        //设置颜色
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        //画图
        g.drawString(rand, 18 * i + 6, 26);
    }

    //输出
    g.dispose();

    //输出
    ImageIO.write(image, "JPEG", response.getOutputStream());
    out.clear();
    out = pageContext.pushBody();
%>